import wx 
import pymysql
from html.parser import HTMLParser 
import re

class Example(wx.Frame): 
    def __init__(self, parent, title,size = (640,480)): 
        super(Example, self).__init__(parent, title = title,size=size) 


        p = wx.Panel(self) 
        mainbox = wx.BoxSizer(wx.VERTICAL) 

        t = wx.StaticBox(parent=p, id=-1, label='Add:') 
        tSizer = wx.StaticBoxSizer(t, wx.VERTICAL)

        
        self.t1 = wx.TextCtrl(p,style=wx.TE_MULTILINE) 
        self.t1.Bind(wx.EVT_LEFT_DCLICK,self.OnclickCls)
        tSizer.Add(self.t1, proportion=1,flag=wx.ALL|wx.EXPAND, border=5)
        
        box = wx.BoxSizer(wx.HORIZONTAL)
        AddButton = wx.Button(p, label='录入' )
        AddButton.Bind(wx.EVT_BUTTON, self.OnClickFind)
        ClsButton = wx.Button(p, label='清空' )
        ClsButton.Bind(wx.EVT_BUTTON, self.OnclickCls)
        self.l1 = wx.StaticText(p)
        box.Add(AddButton, proportion=0, flag=wx.ALL|wx.CENTER, border=5) 
        box.Add(ClsButton, proportion=0, flag=wx.ALL|wx.CENTER, border=5)
        box.Add(self.l1, proportion=0, flag=wx.ALL|wx.CENTER, border=5)
        tSizer.Add(box,proportion=0, flag=wx.ALL|wx.EXPAND, border=10)
        mainbox.Add(tSizer,proportion=1, flag=wx.ALL|wx.EXPAND, border=5)

        self.t2=wx.TextCtrl(p,style=wx.TE_MULTILINE|wx.TE_READONLY)
        mainbox.Add(self.t2, proportion=1, flag=wx.ALL|wx.EXPAND, border=15)
        p.SetSizer(mainbox) 
        #self.Centre() 
        self.Show()
    def OnClickFind(self,event):
        #wx.MessageBox(self.nm1.GetValue())
        parser = MyHTMLParser()
        parser.feeds(self,self.t1.GetValue())

    def OnclickCls(self,event):
        self.t1.SetValue("")
        self.t2.SetValue("")

class MyHTMLParser(HTMLParser):
    def __init__(self):
        HTMLParser.__init__(self)
        self.data = ['root']
        self.q=''
        self.o=''
        self.a=''
    def feeds(self,obj,str):
        self.frm=obj
        self.i=0
        self.r=0
        self.feed(str)
        sql = "SELECT `Q` FROM xf"
        cur.execute(sql)
        db.commit()
        obj.l1.SetLabel("添加：{i}    重复：{r}    总计：{a}".format(i=self.i,r=self.r,a=cur.rowcount))
    def handle_starttag(self, tag, attrs):
        if(tag!='input'):
            self.data.append(tag)
            #print(self.data)

    def handle_endtag(self, tag):
        self.data.pop(-1)
        if(len(self.data)==2):
            sql = "SELECT `Q` FROM xf where `Q` like '%{f}%'" .format(f=self.q)
            cur.execute(sql)
            if(cur.rowcount>0):
                self.r+=1
                #print(self.q,'已存在')
                self.frm.t2.AppendText('\n-----已存在-----\n'+self.q)
            else:
                sql = "INSERT INTO `xf` (`Q`, `O`,`A`) VALUES ('%s', '%s','%s')"%(self.q,self.o,self.a)
                cur.execute(sql)  
                db.commit()
                #print('已添加',self.q)
                self.i+=1
                self.frm.t2.AppendText('\n+++++已添加+++++\n'+self.q)
                
        #print(self.data)

    def handle_data(self, data):
        if(self.data[-1]=='h3'):
            if(data[1]=='、'):
                self.q=data[2:]
            else:
                self.q=data[3:]

            self.o=''
        elif(self.data[-1]=='li' and len(self.data)==5 ):
            self.o=self.o+data+"\n"
        elif(self.data[-1]=='strong'):
            self.a= re.sub(r'用户选择.*$', "", data)
       
app = wx.App() 
parser = MyHTMLParser()
db = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='zthubo',charset='utf8')
cur = db.cursor()
Example(None, title = 'xf.faxuan.net add') 
app.MainLoop()
db.close()